package uestc.lj.basic.tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 层次遍历二叉树
 *
 * @Author:Crazlee
 * @Date:2021/11/23
 */
public class Code02_LevelTraversal {

	public static class Node {
		public int value;
		public Node left;
		public Node right;

		public Node(int data) {
			this.value = data;
		}
	}

	/**
	 * 层次遍历二叉树
	 *
	 * @param head 二叉树头节点
	 */
	public static void levelTraversal(Node head) {
		if (head == null) {
			return;
		}
		Queue<Node> queue = new LinkedList<>();
		queue.add(head);
		while (!queue.isEmpty()) {
			head = queue.poll();
			System.out.print(head.value + " ");
			if (head.left != null) {
				queue.add(head.left);
			}
			if (head.right != null) {
				queue.add(head.right);
			}
		}
		System.out.println();
	}
}
